package logic

import (
	"api/pkg"
	"context"
	"origin_srv/user"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RealNameLogic) RealName(req *types.RealNaemRequest) (resp *types.RegMsgResponse, err error) {
	// todo: 实名认证
	//模拟用户id
	userId := 1
	//调用实名认证接口
	t := pkg.RealName(req.RealName, req.IdCard)
	if t == false {
		return &types.RegMsgResponse{
			Cre:  -1,
			Msg:  "实名认证失败",
			Data: nil,
		}, nil
	}
	//认证成功新增
	_, err = l.svcCtx.UserSrv.RealName(l.ctx, &user.RealNameRequest{
		RealName: req.RealName,
		IdCard:   req.IdCard,
		UserId:   int64(userId),
	})
	if err != nil {
		return &types.RegMsgResponse{
			Cre:  -1,
			Msg:  "服务器错误",
			Data: nil,
		}, err
	}

	return &types.RegMsgResponse{
		Cre:  0,
		Msg:  "实名认证成功",
		Data: nil,
	}, err
}
